home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 301-325 / disk_319 / cnewssrc / cnews.src.lzh / libamiga / gethostname.c < prev    next >
C/C++ Source or Header  |  1980-01-01  |  2KB  |  114 lines

  1. /*
  2.  * v7 gethostname simulation
  3.  *    taken from pathalias and cleaned up.  Thanks, peter.
  4.  */
  5.  
  6. #include <stdio.h>
  7.  
  8. #ifdef AMIGA
  9. #  ifndef index
  10. #    define index    strchr
  11. #    define rindex    strrchr
  12. #  endif
  13. #endif
  14.  
  15. /* imports from libc */
  16. extern char *index(), *strncpy();
  17. extern FILE *fopen(), *popen();
  18.  
  19. /* forwards */
  20. void readhostfile();
  21.  
  22. #define MAXHOST 256
  23.  
  24. #ifndef min
  25. # define min(a,b) ((a) < (b)? (a): (b))
  26. #endif
  27.  
  28. static char defhost[] = "ckctpa";
  29.  
  30. #ifdef AMIGA
  31. static char buff[BUFSIZ];
  32. #else
  33. static char *namefiles[] = {
  34.     "/etc/whoami",
  35.     "/etc/systemid",
  36.     NULL,
  37. };
  38. #endif /* AMIGA */
  39.  
  40. int
  41. gethostname(hostname, size)
  42. register char *hostname;
  43. int size;
  44. {
  45. #ifdef AMIGA
  46.     *hostname = '\0';
  47.  
  48.     /* try the NEWSCTL directory for a file called "whoami" */
  49.     strcpy(buff, ctlfile("whoami"));
  50.     readhostfile(hostname, size, buff);
  51.     if (*hostname != '\0')
  52.         return( 0 );
  53. #else
  54.     register FILE *whoami;
  55.     register char **namep;
  56.  
  57.     *hostname = '\0';
  58.  
  59.     /* try files in namefiles */
  60.     for (namep = namefiles; *namep != NULL; namep++) {
  61.         readhostfile(hostname, size, *namep);
  62.         if (*hostname != '\0')
  63.             return 0;
  64.     }
  65.     /* try /usr/include/whoami.h */
  66.     if ((whoami = fopen("/usr/include/whoami.h", "r")) != NULL) {
  67.         while (!feof(whoami)) {
  68.             char sysname[MAXHOST];
  69.  
  70.             if (fgets(sysname, MAXHOST, whoami) == NULL)
  71.                 break;
  72.             if (sscanf(sysname, "#define sysname \"%[^\"]\"",
  73.                 hostname) > 0)
  74.                 break;
  75.         }
  76.         (void) fclose(whoami);
  77.         if (*hostname != '\0')
  78.             return 0;
  79.     }
  80.     /* ask uucp */
  81.     if ((whoami = popen("PATH=/bin:/usr/bin:/usr/ucb uuname -l", "r")) != NULL) {
  82.         register char *ptr;
  83.  
  84.         (void) fgets(hostname, size, whoami);
  85.         (void) pclose(whoami);
  86.         if ((ptr = index(hostname, '\n')) != NULL)
  87.             *ptr = '\0';
  88.     }
  89.     if (*hostname != '\0')
  90.         return 0;
  91. #endif /* AMIGA */
  92.  
  93.     /* aw hell, i give up!  is this a real unix? */
  94.     (void) strncpy(hostname, defhost, min(sizeof defhost, size));
  95.     return 0;
  96. }
  97.  
  98. static void
  99. readhostfile(hostname, size, file)    /* read host name from file */
  100. char *hostname, *file;
  101. int size;
  102. {
  103.     register FILE *whoami;
  104.  
  105.     if ((whoami = fopen(file, "r")) != NULL) {
  106.         register char *ptr;
  107.  
  108.         (void) fgets(hostname, size, whoami);
  109.         (void) fclose(whoami);
  110.         if ((ptr = index(hostname, '\n')) != NULL)
  111.             *ptr = '\0';
  112.     }
  113. }
  114.